#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
class Student{
public:
    int id;
    char name[10];
    int grade;
    Student(int id, char* name,int grade){
        this->id=id;
        this->grade=grade;
        strcpy(this->name,name);
    }
};
bool c1(const Student& a, const Student& b){
    return a.id<b.id;
}
bool c2(const Student& a, const Student& b){
    int res=strcmp(a.name,b.name);
    if(res==0)
        return a.id<b.id;
    else
        return res<0?true:false;
}
bool c3(const Student& a, const Student& b){
    if(a.grade==b.grade)
        return a.id<b.id;
    else
        return a.grade<b.grade;
}
int main(){
    int n,c;
    scanf("%d %d",&n,&c);
    vector<Student> sv;
    int i=0;
    while(i<n){
        int id,grade;
        char name[10];
        scanf("%d %s %d",&id,name,&grade);
        Student stu(id,name,grade);
        sv.push_back(stu);
        i++;
    }
    if(c==1)
        sort(sv.begin(),sv.end(),c1);
    if(c==2)
        sort(sv.begin(),sv.end(),c2);
    if(c==3)
        sort(sv.begin(),sv.end(),c3);
    for(i=0;i<sv.size();i++){
        printf("%06d %s %d\n",sv[i].id,sv[i].name,sv[i].grade);
    }
    return 0;
}
